import {defineStore} from 'pinia'
import request from '@/utils/request'
import {ApiRes,CategoryItem,TopCategory} from '@/types/data'
import {topCategory} from '@/store/constants'

const defaultCategory = topCategory.map((item)=>{
    return {
        name:item
    }
})

const useCategoryStore  = defineStore('category',{
    state(){
        return {
            list:defaultCategory as CategoryItem[],
            topCategory:{} as TopCategory
        }
    },
    actions:{
        // 获取所有分类数据
        async getAllCategory(){
            const res = await request.get<ApiRes<CategoryItem[]>>("/home/category/head")
            this.list = res.data.result
        },

        // 获取一级分类数据
        async getTopCategory(id:string){
            // const res = await request.get('/category?id='+id)
            const res = await request.get<ApiRes<TopCategory>>('/category',{params:{id}})
            this.topCategory = res.data.result
        }
    },
    getters:{}
})

export default useCategoryStore